package com.yingxin.yx.framework.commons.exception;


import com.yingxin.yx.framework.commons.GlobalInstance;
import com.yingxin.yx.framework.commons.StatusCode;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

/**
 * @author fanchao
 */
@Data
@NoArgsConstructor
public class BizException extends RuntimeException {

    private String code;

    private String msg;

    public BizException(String code) {
        super(code);
        this.code = "BIZ_EX";
        StatusCode statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(code);
        if (statusCode == null) {
            this.msg = code;
        } else {
            this.msg = statusCode.getMessage();
        }
    }

    public BizException(String code, String message) {
        super(message);
        this.code = code;
        StatusCode statusCode = GlobalInstance.getInstance().getStatusCodeMap().get(code);
        if (statusCode == null) {
            this.msg = StringUtils.isEmpty(message) ? "" : message;
        } else {
            this.msg = StringUtils.isEmpty(message) ? statusCode.getMessage() : statusCode.getMessage() + ":" + message;
        }
    }

}